# js通过java控制台命令调用java模块
{ exec } = require('child_process')
path = require('path')
class_path = path.join(process.env.PWD, 'class/') 
class_path.replaceAll('\\','/')
fs = require('fs')
l = console.log
cmd = [
	"java #{class_path}AES.java ",
	"java -jar #{class_path}AES.jar ",
	"javac #{class_path}AES.java",
	"cd #{class_path} && jar --create --verbose --file AES.jar --main-class AES AES.class"
]


# 错误处理
err = (err,cmd) ->
	throw Error("\nERR DESC : AES CIPHER COMPILE FAIL  \nERR IS : #{err}")


#  AES加密
aesEncode = (data, key) ->
	data = String(data)
	unless data.length is 11
		return l('你输入的数据不是正确的手机号')
	CMD = if key then (cmd[1]+data+' '+key) else (cmd[1]+data)
	result = await commend(CMD).catch(err)
	return result.trim()

#  AES数组加密
aesEncodeArr = (data, key) ->
	data = data.join(',')
	CMD = if key then (cmd[1]+data+' '+key) else (cmd[1]+data)
	result = await commend(CMD).catch(err)
	return result.trim().split('\n')


# AES解密
aesDecode = (data, key) ->
	data = String(data)
	unless data.match('==')
		return l('你输入的数据不是base64格式数据')
	CMD = if key then (cmd[1]+data+' '+key) else (cmd[1]+data)	
	result = await commend(CMD).catch(err)
	return result.trim()

# AES数组解密
aesDecodeArr = (data, key) ->
	data = data.join(',')
	unless data.match('==')
		return l('你输入的数据不是base64格式数据')
	CMD = if key then (cmd[1]+data+' '+key) else (cmd[1]+data)	
	result = await commend(CMD).catch(err)
	return result.trim().split('\n')


# 封装控制台命令
commend = (cmd) ->
	return new Promise (resolve, reject) -> 
		exec cmd, (err, out) -> 
			if(err) 
				reject(err)
			resolve(out)

# java文件编译,jar可执行环境配置
compile = () ->
	hasjar = fs.existsSync("#{class_path}AES.jar")
	if !hasjar
		await commend(cmd[2])
		l('class 文件生成成功')	

		await commend('echo "Manifest-Version: 1.0\nMain-Class: AES\n"')
		l('jar 包配置文件生成')


		await commend(cmd[3])
		l('jar 包生成成功')

		await fs.unlinkSync("#{class_path}AES.class")
		l('class 文件删除成功')
	else
		try
			await commend(cmd[1]+'15083325621')
			l('jar包存在并能正常运行')
		catch err
			await fs.unlinkSync("#{class_path}AES.jar")
			l('老jar包删除成功')
			
			await commend(cmd[2])
			l('class 文件生成成功')	

			await commend('echo "Manifest-Version: 1.0\nMain-Class: AES\n"')
			l('jar 包配置文件生成')


			await commend(cmd[3])
			l('jar 包生成成功')

			await fs.unlinkSync("#{class_path}AES.class")
			l('class 文件删除成功')


# 单元测试
unitTest = () ->
	# 第一版基础功能测试，此功能用于combo 控制器
	encrypto = await aesEncode('15083325621')
	decrypto = await aesDecode(encrypto)
	l('--------------------------第一版测试结果开始-------------------------')
	l(encrypto)    # T7KS2G6/LX5HAXbvAuUvEQ==
	l(decrypto)		 # 15083325621
	l('--------------------------第一版测试结果结束-------------------------')

	# 第二版数组功能测试，此功能用于buycard 控制器
	encrypto = await aesEncodeArr(['李金科','15083325621','410325199608130059','田湖镇饭9号'])
	decrypto = await aesDecodeArr(["C+232D0ZC6zbRvRT8/iZYQ==", "jQrvS4I+vmfOAkISQHG+rw==","YDniukuT35lvT02EMK443Q=="],'xljm4nxOgx3BPnFoy1VjYAtjYjQhNK')
	l('--------------------------第二版测试结果开始-------------------------')
	l(encrypto)		# [ 'T7KS2G6/LX5HAXbvAuUvEQ==', 'T7KS2G6/LX5HAXbvAuUvEQ==' ]
	l(decrypto)		# [ '17610008770', '17610007622' ]
	l('--------------------------第二版测试结果结束-------------------------')


# unitTest()
# compile()

module.exports = { 
	aesEncode, 
	aesDecode, 
	compile, 
	aesEncodeArr, 
	aesDecodeArr
}